<?php
// +----------------------------------------------------------------------
// | Author: 闪电 <573725206@qq.com>
// +----------------------------------------------------------------------

namespace Common\Controller;
use Think\Auth;
use Think\Controller;

class AuthController extends Controller {
	protected $model = 'model'; //模型表
	protected $link_list = 'link_list'; //联动模型分类表
	protected $link = 'link'; //联动模型数据表
	protected $user_table_nane = 'user'; //用户表
	protected $auth_group_nane = 'auth_group'; //用户组表
	protected $auth_group_access = 'auth_group_access'; //组权限表
	protected $auth_rule = 'auth_rule'; //规则表
	protected $form = 'Public/_Form'; //表单模板地址

	//Auth 权限控制
	protected function _initialize() {
		$session_auth = session('auth');
		//判断是否进入后台
		if (!strstr($_SERVER['REQUEST_URI'], 'Admin')) {
			$url = U('Admin/Login/index', array('u' => 'Home_Pay_index'));
		} else {
			$url = U('Admin/Login/index');
		}
		if (!$session_auth) {
			$this->error('请先登陆...', $url);
		}
		if ($session_auth['lock']) {
			$this->error('用户已被锁定!');
		}
		/*此ID用户获取全部权限*/
		// if ($session_auth['0']['id']==ADMINISTRATOR_ID) {
		// 	return true;
		// }
		$auth = new Auth();
		//权限检查
		if (!$auth->check(MODULE_NAME . '/' . CONTROLLER_NAME . '/' . ACTION_NAME, $session_auth['id'])) {
			$this->error('没有权限');
		}
		//channel
		$this->assign('channel', $this->AgeList());
		//获取未读公告消息数量
		$this->_getState();

	}
	//获取用户所有权限模块
	public function AgeList() {
		//获取用户组信息
		$list = $this->_authlist();

		$map['id'] = array('in', $list['0']['rules']);
		//设置不显示隐藏模块
		$map['_string'] = 'auth=1';

		$AgeList = M($this->auth_rule)->where($map)->field('condition', true)->order('ord ASC')->cache(true)->select();
		$agedata = $this->_Role_Nav($AgeList);
		//给当前模块添加标识识别
		return $this->_selectedmodel($agedata);

	}
	//获取用户组信息
	private function _authlist() {
		$auth = session('auth');
		$AgeList = new Auth();
		//返回排序后获取的用户组信息 return array()
		return $AgeList->getGroups($auth['id']);
	}
	//给当前模块添加标识
	private function _selectedmodel($agedata, $selected = 'selected') {
		//当前模块地址
		$controller = MODULE_NAME . '/' . CONTROLLER_NAME . '/' . ACTION_NAME;
		//模糊获取当前模块地址
		$controller2 = explode('/', $controller);
		$co = count($controller2) - 1;
		for ($n = 0; $n < $co; $n++) {
			$indexpath .= $controller2[$n] . '/';
		}
		$modelpath = substr($indexpath, 0, strlen($indexpath) - 1);

		//这里是从后台首页下面开始查找 数据第一个数
		$agedata = $agedata['0']['child'];
		foreach ($agedata as $key => $value) {
			if ($agedata[$key]['name'] == $modelpath) {
				$agedata[$key][$selected] = $selected;
				session('nav', $agedata[$key]);
			}
			if ($agedata[$key]['child']) {
				foreach ($agedata[$key]['child'] as $key2 => $value2) {
					if ($agedata[$key]['child'][$key2]['name'] == $controller) {
						$agedata[$key]['child'][$key2][$selected] = $selected;
						session('nav', $agedata[$key]);
					}
				}
			}
		}
		return $agedata;
	}
	//节点导航
	//$role 要处理的节点数组 $userid,跟据用户id查询出与之有关系的数据做标识 $Conditions='0' 从新一个数组开始处理
	protected function _Role_Nav($role, $groupid = false, $auth_group = false, $Conditions = '0') {
		//主要用于给已经加过的权限做标识  此顶可不用
		if ($groupid) {
			$temp = cache_table($auth_group, array('id' => $groupid));
			$rules = $temp['0'];
			//$rules = M($auth_group)->where(array('id' => $groupid))->field('rules')->find();
			//查找出已存在的数据加选中状态
			$array_rules = explode(',', $rules['rules']);
			for ($k = 0; $k < count($role); $k++) {
				if (in_array($role[$k]['id'], $array_rules)) {
					$role[$k]['checked'] = 'checked';
				}
			}
		}
		//返回文件树结构
		return list_to_tree($role, 'id', 'level', 'child');
	}
	/*
		共用模型数据调用
		$number int 模型id
		$docid  int 模型对应表id
	*/

	public function PublicDataModel($number, $docid, $url = false) {
		if (is_int($docid)) {
			$id = $docid;
		}
		$ControllerName = D('Admin/Model');
		//模型条件数据
		if (IS_POST) {

			$data = $ControllerName->ModelDataJson($number, $id);

			if ($data['status']) {
				$this->success($data['info'], $url);
			} else {
				$this->error($data['info']);
			}

		} else {

			$html = $ControllerName->FormLayout($number, $id);
			$this->assign('mid', $number);
			$this->assign('form', $html);
			//
			$this->display($this->form);
		}

	}
	//反回表单html数据
	public function FormHtml($number) {
		if (!is_int($number)) {
			return false;
		}
		$ControllerName = D('Admin/Model');
		$html = $ControllerName->FormLayout($number);

		$this->assign('mid', $number);
		$this->assign('form', $html);
	}
	//获取未读公告消息数量
	private function _getState() {
		$map['state'] = 0;
		$path = 0;
		$state = M(getModelTable(32))->where($map)->field('browstuid')->select();
		for ($i = 0; $i < count($state); $i++) {
			$json = json_decode($state[$i]['browstuid']);
			if (!in_array(getuserid(), $json)) {
				$path += 1;
			}

		}

		$this->assign('state', $path);
	}

}